package com.xin.springclouduser.service.impl;

import com.xin.springclouduser.converter.User2UserDTO;
import com.xin.springclouduser.converter.UserDTO2User;
import com.xin.springclouduser.dataobject.User;
import com.xin.springclouduser.dto.PageResult;
import com.xin.springclouduser.dto.UserDTO;
import com.xin.springclouduser.repository.UserRepository;
import com.xin.springclouduser.service.UserService;
import com.xin.springclouduser.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
@Service
@Slf4j
public class UserServiceImpl implements UserService{
    @Autowired
    private UserRepository userRepository;

    /**
     * 创建用户
     * @param userDTO
     * @return
     */
    @Transactional
    public UserDTO create(UserDTO userDTO) {
        userDTO.setUserId(KeyUtil.genUniqueKey());
        User convert = UserDTO2User.convert(userDTO);
        User save = userRepository.save(convert);
        if(ObjectUtils.isEmpty(save)){
            log.info("【创建用户】失败!");
            return null;
        }
        UserDTO dto = User2UserDTO.convert(save);
        return dto;
    }

    /**
     * 更新用户信息
     * @param userDTO
     * @return
     */
    @Transactional
    public UserDTO update(UserDTO userDTO) {
        UserDTO dto = findOneByUserId(userDTO.getUserId());
        if (ObjectUtils.isEmpty(dto)){
               log.info("【更改用户信息】失败!用户不存在,userId={}",userDTO.getUserId());
               return null;
        }
        dto.setNickName(userDTO.getNickName());
        dto.setHeadImage(userDTO.getHeadImage());
        UserDTO dto1 = create(dto);
        if (ObjectUtils.isEmpty(dto1)){
              log.info("【更改用户信息】失败!");
              return null;
        }
        return dto1;
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @Transactional
    public boolean delete(String uid) {
        UserDTO userDTO = findOneByUserId(uid);
        if (ObjectUtils.isEmpty(userDTO)){
               log.info("【删除用户】失败!用户不存在! userId={}",uid);
               return false;
        }
        User convert = UserDTO2User.convert(userDTO);
        userRepository.delete(convert);
        return true;
    }

    /**
     * 根据UserId查询用户
     * @param userId
     * @return
     */
    public UserDTO findOneByUserId(String userId) {
        User user = userRepository.findOne(userId);
        if (ObjectUtils.isEmpty(user)){
               return null;
        }
        UserDTO convert = User2UserDTO.convert(user);
        return convert;
    }

    /**
     * 根据openId查询用户
     * @param openId
     * @return
     */
    public UserDTO findOneByOpenId(String openId) {
        User user = userRepository.findByOpenId(openId);
        if (ObjectUtils.isEmpty(user)){
            return null;
        }
        UserDTO userDTO = User2UserDTO.convert(user);
        return userDTO;
    }

    /**
     * 查询全部用户
     * @param pageable
     * @return
     */
    public PageResult<User> findAll(Pageable pageable) {
        Page<User> page = userRepository.findAll(pageable);
        PageResult<User> pageResult=new PageResult<>();
        pageResult.setPageSize(page.getSize());
        pageResult.setPageNo(page.getTotalPages());
        pageResult.setTotal(page.getTotalElements());
        pageResult.setContent(page.getContent());
        return pageResult;
    }
}
